package com.visikard.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.visikard.application.VKRegApplication;
import com.visikard.ui.R;
import com.visikard.utility.BitmapUtils;
import com.visikard.utility.UILImageUtils;
import com.visikard.widget.NavigationStateRelativeLayout;

public class MerchantNavigationFragment extends BaseFragment {

	private static final int DELAY_NAVIGATION = 400;

	public interface IMerchantNaviFragment {
		public void closeMenu();

		public void doLogOut();

		public void doCouponBuilder();

		public void doRedeemNow();

		public void doHome();

		public void doRTUICoupon();
	}

	private NavigationStateRelativeLayout lyLogOut, lyCouponBuilder, lyRedeemNow, lyHome, lyRTUICoupon,
			lyCouponVisible;
	private ImageView userAvatar;
	private TextView userProfileName;

	private IMerchantNaviFragment mListener;
	private int mCurrentSelectedId;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		View parent = inflater.inflate(R.layout.merchant_navigation_fragment, container, false);

		lyLogOut = (NavigationStateRelativeLayout) parent.findViewById(R.id.home_navigation_home_logout);
		lyCouponBuilder = (NavigationStateRelativeLayout) parent.findViewById(R.id.home_navigation_home_coupon_builder);
		userAvatar = (ImageView) parent.findViewById(R.id.home_navigation_profile_avatar);
		userProfileName = (TextView) parent.findViewById(R.id.home_navigation_profile_name);
		lyRedeemNow = (NavigationStateRelativeLayout) parent.findViewById(R.id.home_navigation_home_redeem_now);
		lyHome = (NavigationStateRelativeLayout) parent.findViewById(R.id.home_navigation_home_home);
		lyRTUICoupon = (NavigationStateRelativeLayout) parent.findViewById(R.id.home_navigation_home_rtui_coupon);

		if (VKRegApplication.merchantAccountObj != null) {
			UILImageUtils.loadImage(userAvatar, VKRegApplication.merchantAccountObj.getImageSmall(), 1,
					BitmapUtils.ROUND_ALL, 0, 0, 0, 0, 0);
			userProfileName.setText(VKRegApplication.merchantAccountObj.getFname() + " "
					+ VKRegApplication.merchantAccountObj.getLname());
		}

		lyLogOut.setOnClickListener(this);
		lyCouponBuilder.setOnClickListener(this);
		lyRedeemNow.setOnClickListener(this);
		lyHome.setOnClickListener(this);
		lyRTUICoupon.setOnClickListener(this);

		if (VKRegApplication.merchantAccountObj != null
				&& (VKRegApplication.merchantAccountObj.getMerchantIdFromRTUI() == null || VKRegApplication.merchantAccountObj
						.getMerchantIdFromRTUI().isEmpty())) {
			// normal merchant
			lyCouponBuilder.setVisibility(View.VISIBLE);
			lyRTUICoupon.setVisibility(View.GONE);
			lyCouponVisible = lyCouponBuilder;
		}
		else {
			// RTUI merchant
			lyCouponBuilder.setVisibility(View.GONE);
			lyRTUICoupon.setVisibility(View.VISIBLE);
			lyCouponVisible = lyRTUICoupon;
		}

		lyRedeemNow.setChecked(true);
		mCurrentSelectedId = R.id.home_navigation_home_home;

		return parent;
	}

	public void setListener(IMerchantNaviFragment listener) {
		mListener = listener;
	}

	private void reset(int id) {
		switch (id) {
			case R.id.home_navigation_home_logout:
				lyLogOut.setChecked(false);
				return;
			case R.id.home_navigation_home_coupon_builder:
				lyHome.setChecked(false);
				lyRedeemNow.setChecked(false);
				lyLogOut.setChecked(false);
				lyCouponBuilder.setChecked(true);
				return;
			case R.id.home_navigation_home_rtui_coupon:
				lyHome.setChecked(false);
				lyRedeemNow.setChecked(false);
				lyLogOut.setChecked(false);
				lyRTUICoupon.setChecked(true);
				return;
			case R.id.home_navigation_home_redeem_now:
				lyHome.setChecked(false);
				lyCouponVisible.setChecked(false);
				lyLogOut.setChecked(false);
				lyRedeemNow.setChecked(true);
				return;
			case R.id.home_navigation_home_home:
				lyCouponVisible.setChecked(false);
				lyRedeemNow.setChecked(false);
				lyLogOut.setChecked(false);
				lyHome.setChecked(true);
				return;
		}
	}

	@Override
	public void onClick(View v) {
		if (mCurrentSelectedId == v.getId()) {
			reset(v.getId());
			if (mListener != null) {
				mListener.closeMenu();
			}
			return;
		}

		switch (v.getId()) {
			case R.id.home_navigation_home_logout:
				reset(v.getId());
				mCurrentSelectedId = v.getId();
				if (mListener != null) {
					mListener.closeMenu();
					mListener.doLogOut();
				}
				return;
			case R.id.home_navigation_home_coupon_builder:
				reset(v.getId());
				mCurrentSelectedId = v.getId();
				if (mListener != null) {
					mListener.closeMenu();
					mHandler.postDelayed(new Runnable() {

						@Override
						public void run() {
							mListener.doCouponBuilder();
						}
					}, DELAY_NAVIGATION);
				}
				return;
			case R.id.home_navigation_home_rtui_coupon:
				reset(v.getId());
				mCurrentSelectedId = v.getId();
				if (mListener != null) {
					mListener.closeMenu();
					mHandler.postDelayed(new Runnable() {

						@Override
						public void run() {
							mListener.doRTUICoupon();
						}
					}, DELAY_NAVIGATION);
				}
				return;
			case R.id.home_navigation_home_redeem_now:
				reset(v.getId());
				mCurrentSelectedId = v.getId();
				if (mListener != null) {
					mListener.closeMenu();
					mHandler.postDelayed(new Runnable() {

						@Override
						public void run() {
							mListener.doRedeemNow();
						}
					}, DELAY_NAVIGATION);
				}
				return;
			case R.id.home_navigation_home_home:
				reset(v.getId());
				mCurrentSelectedId = v.getId();
				if (mListener != null) {
					mListener.closeMenu();
					mHandler.postDelayed(new Runnable() {

						@Override
						public void run() {
							mListener.doHome();
						}
					}, DELAY_NAVIGATION);
				}
				return;
		}
		super.onClick(v);
	}

}
